<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page session="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<%!

public String buildUrlString(HttpServletRequest request, String name) {
    String title = Character.toUpperCase(name.charAt(0)) + name.substring(1);
    String order = (String) request.getAttribute("order");
    boolean ascending = (Boolean) request.getAttribute("ascending");

    String url = request.getRequestURL().toString() + "/../";

    String start = "<table><tr><td><a href=\"user?order=" + name;
    String end = "</a></td></tr></table>";

    if (order.equals(name)) {
        if (ascending) {
            return start + "&ascending=false\"><b>" + title
                    + "</b></td><td><img src=\"" + url + "desc.gif\"/>" + end;
        } else {
            return start + "&ascending=true\"><b>" + title
                    + "</b></td><td><img src=\"" + url + "asc.gif\"/>" + end;
        }
    } else {
         return start + "\">" + title + end;
    }
}

%>

<html>

<head>
    <style>
a {
color: blue;
}
    </style>
</head>

<c:if test="${not empty users}">
<h2>User list</h2>
</c:if>

<table border="1">
<c:if test="${not empty users}">
    <tr>
        <th><% out.write(buildUrlString(request, "name")); %></th>
        <th><% out.write(buildUrlString(request, "phone")); %></th>
        <th><% out.write(buildUrlString(request, "birthday")); %></th>
        <th>Action</th>
    </tr>
</c:if>
<c:forEach items="${users}" var="u">
    <tr>
        <td><c:out value="${u.name}"/></td>
        <td><c:out value="${u.phone}"/></td>
        <td><c:out value="${u.birthday}"/></td>
        <td>
            <table border="0">
                <tr>
                    <td><a href='<c:url value="user/${u.id}/edit"/>'>Edit</a></td>
                    <td><a href='<c:url value="user/${u.id}/delete"/>'>Delete</a></td>
                </tr>
            </table>
        </td>
    </tr>
</c:forEach>
</table>
<br>
<h2>Add user</h2>
<form:form modelAttribute="user">
        <form:hidden path="id"/>
    <table>
        <tr>
            <td>Name</td>
            <td><form:input path="name"/><form:errors path="name" /></td>
        </tr>
        <tr>
            <td>Email</td>
            <td><form:input path="phone"/><form:errors path="phone" /></td>
        </tr>
        <tr>
            <td>Birth</td>
            <td><form:input path="birthday"/><form:errors path="birthday" /></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit"></td>
        </tr>
    </table>
</form:form>
</html>